package com.yun.zf.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfOwnerCertificateMapper;
import com.yun.zf.admin.service.ZfHouseDocumentInfoService;
import com.yun.zf.admin.service.ZfHouseScoreService;
import com.yun.zf.admin.service.ZfOwnerCertificateService;
import com.yun.zf.common.admin.dto.house.OwnerCertificateDto;
import com.yun.zf.common.admin.model.house.DoucumentInfoMo;
import com.yun.zf.common.admin.model.house.OwnerCertificateMo;
import com.yun.zf.common.admin.model.pact.PactPersonMo;
import com.yun.zf.common.app.domain.ZfHouseScore;
import com.yun.zf.common.app.domain.ZfOwnerCertificate;
import com.yun.zf.common.mapstruct.admin.HouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 业主证件信息表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-03-05
 */
@Service
public class ZfOwnerCertificateServiceImpl extends ServiceImpl<ZfOwnerCertificateMapper, ZfOwnerCertificate> implements ZfOwnerCertificateService {

    @Autowired
    private ZfHouseDocumentInfoService documentInfoService;
    @Autowired
    private ZfHouseScoreService scoreService;


    /**
     * 补充身份证信息
     *
     * @param ownerCertificateMo
     * @return
     */
    @Transactional
    @Override
    public int addOwnerCertificate(OwnerCertificateMo ownerCertificateMo, Long provinceCode, Long cityCode) {
        ZfOwnerCertificate certificate = HouseMapper.INSTANCE.certiificateMotoOwner(ownerCertificateMo);
        certificate.setCityCode(cityCode);
        certificate.setProvinceCode(provinceCode);
        certificate.setCreateTime(new Date());
        // 保存证件信息
        int cn = baseMapper.insert(certificate);
        if (cn>0){
            calculateScore(Long.parseLong(ownerCertificateMo.getBasicId()),cityCode,provinceCode);
        }
        return  cn;
    }

    /**
     * 修改业主身份证信息
     *
     * @param ownerCertificateMo
     * @return
     */
    @Override
    public int motifyOwnerCertificate(OwnerCertificateMo ownerCertificateMo, Long provinceCode, Long cityCode) {
        // 1、业主身份证信息
        ZfOwnerCertificate certificate = HouseMapper.INSTANCE.certiificateMotoOwner(ownerCertificateMo);
        certificate.setCityCode(cityCode);
        certificate.setProvinceCode(provinceCode);
        QueryWrapper<ZfOwnerCertificate> ownerCertificateQueryWrapper = new QueryWrapper<>();
        ownerCertificateQueryWrapper.lambda().eq(ZfOwnerCertificate::getId, certificate.getId())
                .eq(ZfOwnerCertificate::getCityCode, certificate.getCityCode())
                .eq(ZfOwnerCertificate::getProvinceCode, certificate.getProvinceCode());
        ZfOwnerCertificate owner = baseMapper.selectOne(ownerCertificateQueryWrapper);
        BeanUtil.copyProperties(certificate, owner, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        // 更新业主信息
        int cn = baseMapper.update(owner, ownerCertificateQueryWrapper);
        return cn;
    }


    /**
     * 获取业主证件信息
     *
     * @param basicId
     * @param provinceCode
     * @param cityCode
     * @return
     */
    @Override
    public OwnerCertificateDto getOwnerCertificate(Long basicId, Long provinceCode, Long cityCode) {
        // 获取也只证件信息
        QueryWrapper<ZfOwnerCertificate> ownerCertificateQueryWrapper = new QueryWrapper<>();
        ownerCertificateQueryWrapper.lambda().eq(ZfOwnerCertificate::getBasicId, basicId).eq(ZfOwnerCertificate::getCityCode, cityCode)
                .eq(ZfOwnerCertificate::getProvinceCode, provinceCode);
        List<ZfOwnerCertificate> list = baseMapper.selectList(ownerCertificateQueryWrapper);
        ZfOwnerCertificate certificate = list.size() > 0 ? list.get(0) : null;
        // 获取图片信息
        List<DoucumentInfoMo> infoMoList = documentInfoService.getDocumentInfo(basicId, provinceCode, cityCode, 2);
        OwnerCertificateDto dto = new OwnerCertificateDto();
        if (certificate != null) {
            OwnerCertificateMo ownerCertificateMo = HouseMapper.INSTANCE.certiificateToMo(certificate);
            dto.setOwnerCertificateMo(ownerCertificateMo);
        }
        dto.setDocumentInfoList(infoMoList);
        return dto;
    }

    @Override
    public List<PactPersonMo> getOwner(Long basicId, Long provinceCode, Long cityCode) {

        return baseMapper.getPactPerson(basicId, provinceCode, cityCode);
    }

    /**
     * 业主信息上传进度和得分
     * @param basicId
     * @param cityCode
     * @param provCode
     */
    public void calculateScore( Long basicId, Long cityCode, Long provCode) {
        // 电梯权重占30%， 楼层占70%(其中底层10%，低层20%,中楼层25%，高楼层30%，顶层15% )
        ZfHouseScore houseScore = new ZfHouseScore();
        houseScore.setOwnScore(new BigDecimal("10"));
        houseScore.setOwn(new BigDecimal("100"));
        houseScore.setBasicId(basicId);
        houseScore.setCityCode(cityCode);
        houseScore.setProvinceCode(provCode);
        // 更新房源分数
        scoreService.addOrUpdate(houseScore);

    }
}
